﻿using System;
using System.Linq;
using System.Web;
using BL.Abstract;
using BL.Generic;
using DAL.Repositories;
using Model;

namespace BL.Class
{
    public class UsersBL : IUsersBL
    {
        private readonly UnitOfWork unitOfWork = new UnitOfWork();

        public GenericResult<USERS> GetAllUsers()
        {
            try
            {
                return new GenericResult<USERS>
                {
                    ObjectAnswer = unitOfWork.UsersRepository.Get(),
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<USERS>
                {
                    Exception = ex.Message,
                    ObjectAnswer = null,
                    BlAnswer = Answer.Error
                };
            }
        }

        public GenericResult<bool> Insert(USERS user)
        {
            try
            {
                unitOfWork.UsersRepository.Insert(user);
                unitOfWork.Save(HttpContext.Current.User.Identity.Name);
                return new GenericResult<bool>
                {
                    SingleObjectAnswer = true,
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<bool>
                {
                    Exception = ex.Message,
                    ObjectAnswer = null,
                    SingleObjectAnswer = false,
                    BlAnswer = Answer.Error
                };
            }
        }

        public GenericResult<USERS> GetUserById(int userId)
        {
            try
            {
                return new GenericResult<USERS>
                {
                    SingleObjectAnswer = unitOfWork.UsersRepository
                        .Get(i => i.USERID == userId)
                        .FirstOrDefault(),
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<USERS>
                {
                    Exception = ex.Message,
                    ObjectAnswer = null,
                    BlAnswer = Answer.Error
                };
            }
        }

        public GenericResult<bool> Update(USERS user)
        {
            try
            {
                unitOfWork.UsersRepository.Update(user);
                unitOfWork.Save(HttpContext.Current.User.Identity.Name);
                return new GenericResult<bool>
                {
                    SingleObjectAnswer = true,
                    BlAnswer = Answer.Success
                };
            }
            catch (Exception ex)
            {
                return new GenericResult<bool>
                {
                    Exception = ex.Message,
                    SingleObjectAnswer = false,
                    BlAnswer = Answer.Error
                };
            }
        }
    }
}
